public class RemoveNthNodeFromEndList {
    public ListNode removeNthFromEnd(ListNode head, int n) {
        ListNode virtualNode = new ListNode(0, head); // 让虚拟节点作为 head 的前驱节点
        ListNode slow = virtualNode;
        ListNode fast = head;

        // 先让 fast 走 n 步
        while (n-- != 0) {
            fast = fast.next;
        }

        // slow 和 fast 同时走
        while (fast != null) {
            slow = slow.next;
            fast = fast.next;
        }

        // 删除 slow 后面的那个节点
        slow.next = slow.next.next;
        return virtualNode.next;
    }
}
